insheet using "${data}\${inputFile}", names comma clear
set more off

foreach v of varlist * {
	capture confirm numeric variable `v'
		if !_rc {
		di "`v' is numeric"
		}
		else {
		di "`v' changed to numeric"
		cap drop temp
		cap gen temp=real(`v')
		cap drop `v'
		qui gen `v'=temp
		cap drop temp
		}
}
gsort calendar
cap drop temp
drop if back_info_ratio==.
gsort calendar_month fundid
by calendar_month: gen  temp= _n // plan to drop data if super short
by calendar_month: egen temp1= max(temp)
drop if temp1<=4
gsort fundid calendar_month
capture confirm new variable month
if _rc==0{
bys fundid: gen age = calendar_month // if month is missing, age is equal to calendar time
}
drop temp temp1
capture confirm new variable eadm
if _rc==0{
bys fundid: egen eadm = max(adm) // eventually admitted
}
capture confirm new variable logaum
if _rc==0{
bys fundid: gen logaum = aum  
}
////////////////////////////////////////////////////////////////////////////////

winsor back_info_ratio, gen(back_info_ratiow) p(0.0125)
gsort calendar_month back_info_ratiow 
egen irq=xtile(back_info_ratiow), nq(3) by(calendar_month) 

gsort fundid age
gen idobs= _n
save temp0, replace

*create matching bank of uninvested fund-months 
use temp0, clear
keep if eadm==0
save temp1, replace


*matching step
use temp0, clear
keep if eadm==1
*take the first post-investment months of invested funds
bys fundid: keep if _n==1  
*append the mathcbank
append using temp1
cap drop cl_best* cd_best*
gen cl_b1=.
gen cl_b2=.
gen cl_b3=.
*pick closest three by age info-ratio and logaum
mahapick logaum back_info_ratiow age, idvar(idobs) treated(eadm) pickids(cl_b1 cl_b2 cl_b3) clear matchon(calendar_month)
keep if cl_b1!=.
keep idobs cl_b?
reshape long cl_b, i(idobs) j(j)
save tempm, replace //matched fund-months observations



**postinvestment fund-months appended with matched uninvested funds over the respected calendar months
use tempm, clear
keep idobs
	qui gen j=0
	qui gen cl_b=idobs
duplicates drop  
append using tempm //append invested fundids to matches 
	gsort idobs j 
save tempm1, replace
*create map between idobs and fundid
use tempm1, clear
	keep cl_b
	rename cl_b idobs 
duplicates drop
merge 1:1 idobs using temp0, keepus(fundid) keep(matched) nogen
	rename idobs cl_b
save obs2fundid_map, replace
*panel consturction
use tempm1, clear
	gsort idobs j 
merge m:1 idobs using temp0, keepus(fundid) keep(matched) nogen 
joinby fundid using temp0 // invested months
	keep j cl_b calendar_month 
joinby cl_b using obs2fundid_map //get fundids
merge m:1 fundid calendar_month using temp0, keep(matched) // get data for selected fundid months 

if "${simData}"=="Yes" {
gen laum=aum
}
else{
gen logaum=log(aum)
winsor logaum, gen(laum) p(0.0125) // winsorize outlier large funds
}
gen laum_bb_temp= laum if xret_style_nr!=. // focus only where return data available
bysort fundid (age): replace laum_bb_temp= sum(laum_bb_temp) / sum(laum_bb_temp < .)
by fundid: gen laum_bb= laum_bb_temp[_n-1]
replace laum_bb= . if xret_style_nr==.
drop laum_bb_temp

save mpost_panel, replace
